package com.common.utils.BaseUtils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;


public class ListUtil {

    public static <T> Boolean isNull(List<T> list) {
        if (list == null && list.size() == 0) {
            return true;
        }

        return false;
    }

    public static <T> Boolean isNotNull(List<T> list) {
        return !isNull(list);
    }

    public static <X, T> List<T> toChangeList(List<X> list, String name, Class itemClass) {
        List<T> result = new ArrayList<>();
        for (X item : list) {
            Class clazz = item.getClass();
            PropertyDescriptor property = null;
            try {
                property = new PropertyDescriptor(name, clazz);
            } catch (IntrospectionException e) {
                continue;
            }
            try {
                Method method = property.getReadMethod();
                Object invokeData = method.invoke(item);

                if (property.getPropertyType().toString().equals(itemClass.toString())) {
                    result.add((T) invokeData);
                }

            } catch (Exception ex) {
                continue;
            }
        }

        return result;
    }

    public static <T> List<Long> toLongList(List<T> list, String field) {
        return toChangeList(list, field, Long.class);
    }

    public static <T> List<String> toStringList(List<T> list, String field) {
        return toChangeList(list, field, String.class);
    }

   

}
